package top.bigdata.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.bigdata.constants.ZoneConstant;
import top.bigdata.mapper.AuthorMapper;
import top.bigdata.model.dto.author.HotAuthor;
import top.bigdata.model.dto.author.MonthlyAuthor;
import top.bigdata.model.entity.Author;
import top.bigdata.service.api.AuthorService;
import top.bigdata.service.api.ResultService;
import top.bigdata.service.request.UserInfoService;

import java.util.Comparator;
import java.util.List;
import java.util.Random;

@Service
@RequiredArgsConstructor
public class AuthorServiceImpl implements AuthorService {
    private final AuthorMapper authorMapper;
    private final ResultService resultService;
    private final UserInfoService userInfoService;

    @Override
    public List<HotAuthor> getHotAuthors(int tid) {
        var tids = ZoneConstant.tidToTids(tid);
        return resultService.cachedQueryList("hot_author_" + tid, () -> authorMapper.getHotAuthors(tids),
                results -> {
                    var random = new Random();
                    try { // 获取作者信息
                        for (var result : results) {
                            Thread.sleep(random.nextLong(75,200)); // 随机延迟
                            var authorInfo = userInfoService.getUserInfo(result.getMid());
                            result.fromAuthorInfo(authorInfo);
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                , HotAuthor.class);
    }

    @Override
    public List<HotAuthor> getHotAuthorsByTag(String tag){
        return resultService.cachedQueryList( "hot_author_tag_" + tag, () -> authorMapper.getHotAuthorsByTag(tag),
                results -> {
                    var random = new Random();
                    try { // 获取作者信息
                        for (var result : results) {
                            Thread.sleep(random.nextLong(75,200)); // 随机延迟
                            var authorInfo = userInfoService.getUserInfo(result.getMid());
                            result.fromAuthorInfo(authorInfo);
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                , HotAuthor.class);
    }

    /**
     * 获取作者月度数据
     */
    @Override
    public List<MonthlyAuthor> getDataMonthly(long mid) {
        // 只查一次作者静态信息
        Author author = userInfoService.getUserInfo(mid);
        var result = resultService.cachedQueryList(
                "monthly_author_" + mid,          // 缓存 key
                () -> authorMapper.getDataMonthly(mid), // 数据库月度数据
                results -> {          // 补全作者信息
                    // 写回每条月度记录
                    for (MonthlyAuthor m : results) {
                        userInfoService.copyAuthorFields(author, m);
                    }
                },
                MonthlyAuthor.class               // 元素类型
        );

        // 补充没有数据的月份
        var months = new String[]{"2024-09", "2024-10", "2024-11", "2024-12", "2025-01", "2025-02", "2025-03", "2025-04", "2025-05", "2025-06", "2025-07", "2025-08"};
        if (result.size() < months.length) {
            for (String month : months) {
                if (result.stream().noneMatch(res -> res.getMonth().equals(month))) { // 没有该月数据
                    var newMonthlyData = new MonthlyAuthor();
                    newMonthlyData.setMonth(month);
                    userInfoService.copyAuthorFields(author, newMonthlyData);
                    result.add(newMonthlyData);
                }
            }
        }
        // 排序
        result.sort(Comparator.comparing(MonthlyAuthor::getMonth));
        return result;
    }
}
